#region Imported Namespaces

using System;
using System.Data;
using PHSRAG.Utility;
using BO = PHSRAG.Insight21.BusinessObjects;
using PHSRAG.Insight21.CDS;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;

#endregion

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// Provides common state and behavior to the relationship between a person staffed to a study, and 
	/// the relationship of a person who simply has access to the study
	/// </summary>
	public abstract class BaseStudyPerson : BO.SmartBusinessObject
	{
		#region Constants

		private const string BaseStudyPersonKey = "BaseStudyPersonKey_574E3124-0394-4277-889C-613D4151ED2B";

		#endregion

		#region Instance Variables (private)

		private bool isContactPerson;
		private int permissionLevel;
		private string protocolNumber;
		private int protocolId;
		private Person person;
		private int admPersonId;
        
		#endregion

		/// <summary>
		/// Gets the person associated with this relationship
		/// </summary>
		public Person Person
		{
			get
			{
				if (person == null)
				{
					person = new Person(CachePolicy);
					if (admPersonId != 0)
						person.Load(DataAccess.GetConnectionString(), admPersonId);
				}
				return person;
			}
		}

		/// <summary>
		/// Id of the person associated with this relationship, in the database
		/// </summary>
		public int AdmPersonId
		{
			get { return admPersonId; }
			set { SetDirty(ref admPersonId, value); }
		}

        /// Get/Set person's email address
        public string EmailAddress
        {
            get { return Person.Email; }
        }

		/// <summary>
		/// Get the lastname of the person, available here for databinding purposes and convenience
		/// </summary>
		public string LastName
		{
			get { return Person.LastName; }
		}
		/// <summary>
		/// Get the firstname of the person, available here for databinding purposes and convenience
		/// </summary>
		public string FirstName
		{
			get { return Person.FirstName; }
		}

		/// <summary>
		/// Used for display purposes - [LastName, FirstName]
		/// </summary>
		public string FullName
		{
			get { return Person.FullName; }
		}
		/// <summary>
		/// Used for display purposes - [LastName, FirstName (UserName)]
		/// </summary>
		/// <returns>formats "last name, first name"</returns>
		public string DisplayName
		{
			get { return string.Format("{0}, {1} ({2})", LastName, FirstName, Username); }
		}

		/// <summary>
		/// Gets the username of the person, available here for databinding purposes
		/// </summary>
		public string Username
		{
			get { return Person.Username; }
		}

		/// <summary>
		/// Gets the UserId of the person, available here for databinding purposes
		/// </summary>
		public int UserId
		{
			get { return Person.UserId; }
		}

		/// <summary>
		/// Get/Set whether the person receives mail concerning the study
		/// </summary>
		public bool IsContactPerson
		{
			get { return isContactPerson; }
			set { SetDirty(ref isContactPerson, value); }
		}

		/// <summary>
		/// Get/Set the level of access the person has to the study
		/// </summary>
		public int PermissionLevel
		{
			get { return permissionLevel; }
			set { SetDirty(ref permissionLevel, value); }
		}

		/// <summary>
		/// Get/Set the protocolnumber the person is staffed or has access to
		/// </summary>
		public string ProtocolNumber
		{
			get { return protocolNumber; }
			set { SetDirty(ref protocolNumber, value); }
		}

		/// <summary>
		/// Get/Set the id of the protocol the person is staffed or has access to
		/// </summary>
		public int ProtocolId
		{
			get { return protocolId; }
			set { SetDirty(ref protocolId, value); }
		}

		/// <summary>
		/// Initializes the base with the specified caching policy, then sets its state based on
		/// the row passed in.  
		/// </summary>
		/// <param name="cachePolicy">policy to use to cache this instance</param>
		/// <param name="row">row containing data that the person should be initialized with</param>
		protected BaseStudyPerson(ICachePolicy cachePolicy, DataRow row)
			: base(cachePolicy)
		{
			// if this is a new person, contact person and Id won't exist in the row yet
			Id = (row.Table.Columns.Contains("Id")) ? (int)row["Id"] : 0;

			object o = row["ProtocolNumber"];
			ProtocolNumber = (o == DBNull.Value) ? null : (string)o;

			if (row.Table.Columns.Contains("ContactPerson"))
				IsContactPerson = Convert.ToBoolean(row["ContactPerson"]);

			if (row.Table.Columns.Contains("AccessBitValue"))
				PermissionLevel = (int)row["AccessBitValue"];

			if (row.Table.Columns.Contains("ProtocolId"))
				ProtocolId = (int)row["ProtocolId"];

			admPersonId = (int)row["AdmPersonId"];
		}

		/// <summary>
		/// NEW object constructor; initializes the base with the specified cache policy
		/// </summary>
		/// <param name="cachePolicy">policy to use to cache this instance</param>
		protected BaseStudyPerson(ICachePolicy cachePolicy)
			: base(cachePolicy)
		{
		}

		/// <summary>
		/// Determines whether this instance has data that has changed on the object that has
		/// not been persisted in the database
		/// </summary>
		/// <returns>true if changed and not persisted; false otherwise</returns>
		public override bool IsDirty()
		{
			return (Person.Dirty || dirty);
		}

	}
}
 